/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "MYI2C.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "adc_config.h"
#include "string.h"
#include "include.h"
#include "MYI2C.h"
#include "modbus.h"
//#include "SDdriver.h"
#include "adc_config.h"
/* USER CODE END Includes */




/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
//SPI_HandleTypeDef hspi1;


/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
extern void UART6_LowPower( void );
extern void UART7_LowPower( void );
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t  Own_Address = 0;


/* USER CODE END 0 */
//Tout = 1000ms/(40khz/pre) = ms    
//Tout = 1000ms/(40000/256) = 6.4ms * 4095 = 26208ms(MAX) 
#if IWDG_ENABLE
IWDG_HandleTypeDef hiwdg ;

void FEED_IWDG(void)
{
	HAL_IWDG_Refresh(&hiwdg);
}

static void MX_IWDG_Init(void)
{

  /* USER CODE BEGIN IWDG_Init 0 */

  /* USER CODE END IWDG_Init 0 */

  /* USER CODE BEGIN IWDG_Init 1 */

  /* USER CODE END IWDG_Init 1 */
  hiwdg.Instance = IWDG;
  hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
  hiwdg.Init.Reload = 1000;//1000 = 8s 500 = 5s
  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN IWDG_Init 2 */

  /* USER CODE END IWDG_Init 2 */
}
#endif

static void Init_Address(void)
{
	
	Own_Address = (UINT8)(GPIOA->IDR & 0x000F);
	Own_Address = ~Own_Address & 0x0F;
	
  if(!Own_Address)
	{
		Own_Address = 1;
	}
}

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  uint32_t  AD_value,i;
  /* USER CODE END 1 */
  

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();
  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
//	TP0164_ch(37);
#ifdef 	AM5986
	for(i = 0;i < 50;i++){
			TP0164_ch(i);
		  HAL_Delay(1);	
	}
#endif		
	Init_Address();
	TL2543_init();
  MX_TIM2_Init();
	HAL_TIM_Base_Start_IT(&htim2);	
  MX_USART1_UART_Init();  
  MX_USART2_UART_Init();	
  /* USER CODE BEGIN 2 */
	uart_fifo_init();
	MX_SPI1_Init();  printf("Own_Address =");
	ADS1220_Init(&hads1220, &hspi1, GPIOA, GPIO_PIN_8);
//  stm32_adc_init();
#if IWDG_ENABLE	
  MX_IWDG_Init();
#endif
  /* USER CODE END 2 */	
  CD4067_init();
#ifdef I2C_MODE			
  MYI2C_Init(&sersor_stu[0],AD2543_SDI_SDA1_Port,AD2543_SDI_SDA1_Pin,AD2543_NCS_SCL1_Port,AD2543_NCS_SCL1_Pin,0x48,20,10*2*2*2,10*2*2*2);
//  MYI2C_Init(&sersor_stu[1],AD2543_SCK_SDA2_Port,AD2543_SCK_SDA2_Pin,AD2543_SDO_SCL2_Port,AD2543_SDO_SCL2_Pin,0x50,20*10,10*2*2*2,10*2*2*2);
#endif	

//	void GetAllADValue(void);
//	GetAllADValue();	
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	printf("Own_Address =  %d\r\n",Own_Address);
  while (1)
  {

#if IWDG_ENABLE            
	  FEED_IWDG();
#endif

		uart_pro();
    uart2_pro();
    
    // 检查Modbus线圈状态并控制继电器
    static uint8_t coil_state;
    coil_state = xMBUtilGetBits(ucRegCoilsBuf, 0, 1); // 读取第一个线圈的状态
    if (coil_state > 0) {
        POWER_ON;  // 如果线圈为1，打开继电器
    } else {
        POWER_OFF; // 如果线圈为0，关闭继电器
    }
    
//		stm32_adc_enabled(&ADC_Handler,ADC_CHANNEL_5,1);
//		stm32_get_adc_value(&ADC_Handler,ADC_CHANNEL_5,&AD_value);
//		printf("AD_value = %d\n",AD_value * 3300 / 4096);
//		HAL_Delay(100);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
//void SystemClock_Config(void)
//{
//  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
//  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

//  /** Initializes the CPU, AHB and APB busses clocks 
//  */
//  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
//  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
//  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
//  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
//  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
//  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
//  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
//  {
//    Error_Handler();
//  }
//  /** Initializes the CPU, AHB and APB busses clocks 
//  */
//  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
//                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
//  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
//  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
//  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
//  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

//  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
//  {
//    Error_Handler();
//  }
//}

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;//LSI-���Ź�40Khz  HSE-72Mhz  LSE-�ⲿ����32.768Khz  |RCC_OSCILLATORTYPE_LSE
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
	RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
//void SystemClock_Config(void)
//{
//  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
//  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

//  /** Initializes the RCC Oscillators according to the specified parameters
//  * in the RCC_OscInitTypeDef structure.
//  */
//  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
//  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
//  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
//  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
//  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
//  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
//  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
//  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
//  {
//    Error_Handler();
//  }
//  /** Initializes the CPU, AHB and APB buses clocks
//  */
//  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
//                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
//  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
//  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
//  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
//  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

//  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
//  {
//    Error_Handler();
//  }
//}
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  GPIO_InitStruct.Pin = ADS1220_CS_Pin|LED_C_Pin | POWER_C_Pin | BUZZER_Pin | RS485_DIR_Pin | RS485_UART2_DIR_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(LED_C_Port, &GPIO_InitStruct);	
	HAL_GPIO_WritePin(LED_C_Port, LED_C_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(POWER_C_Port, POWER_C_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(BUZZER_Port, BUZZER_Pin, GPIO_PIN_SET);
	HAL_GPIO_WritePin(RS485_DIR_Port, RS485_DIR_Pin, GPIO_PIN_RESET);//Ĭ�Ͻ���
  HAL_GPIO_WritePin(RS485_UART2_DIR_Port, RS485_UART2_DIR_Pin, GPIO_PIN_RESET);//Ĭ�Ͻ���
  HAL_GPIO_WritePin(ADS1220_CS_Port, ADS1220_CS_Pin, GPIO_PIN_SET);
	
//	GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
//  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//  GPIO_InitStruct.Pull = GPIO_PULLUP;
//  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
//	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7, GPIO_PIN_SET);	
//	while(1);
  GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(HC595_DATA_Port, &GPIO_InitStruct);		
	HAL_GPIO_WritePin(HC595_DATA_Port, HC595_SCLK_Pin, GPIO_PIN_SET);	
	HAL_GPIO_WritePin(HC595_DATA_Port, HC595_HCLK_Pin, GPIO_PIN_SET);	
	HAL_GPIO_WritePin(HC595_DATA_Port, HC595_DATA_Pin, GPIO_PIN_SET);	

  GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(BIT1_Port, &GPIO_InitStruct);	
	
  GPIO_InitStruct.Pin = GPIO_PIN_0; //ASD1220 RDY
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);		

  GPIO_InitStruct.Pin = AD2543_SCK_Pin | AD2543_SDI_Pin | AD2543_NCS_Pin | AD2543_SDO_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(AD2543_SCK_Port, &GPIO_InitStruct);		
  HAL_GPIO_WritePin(AD2543_SCK_Port, AD2543_SCK_Pin, GPIO_PIN_RESET);	
  HAL_GPIO_WritePin(AD2543_SDI_Port, AD2543_SDI_Pin, GPIO_PIN_RESET);		
	HAL_GPIO_WritePin(AD2543_NCS_Port, AD2543_NCS_Pin, GPIO_PIN_RESET);	
	HAL_GPIO_WritePin(AD2543_SDO_Port, AD2543_SDO_Pin, GPIO_PIN_RESET);	
	
	BUZZER_ON;
	HAL_Delay(500);
	BUZZER_OFF;
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
